//
//  WYMomentShareInfoView.m
//  FriendCircleDemo
//
//  Created by mac on 2018/10/10.
//  Copyright © 2018年 WY. All rights reserved.
//

#import "WYMomentShareInfoView.h"

@interface WYMomentShareInfoView ()
/// avatarView
@property (weak, readwrite, nonatomic)  UIImageView *avatarView;
/// playBtn
@property (weak, readwrite, nonatomic)  UIButton *playBtn;
/// titleLabel
@property (weak, readwrite, nonatomic)  UILabel *titleLabel;
/// detailLabel
@property (weak, readwrite, nonatomic)  UILabel *detailLabel;
@end

@implementation WYMomentShareInfoView

+(instancetype)shareInfoView
{
    return [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = color(243, 243, 245);
        self.clipsToBounds = YES;
        // 创建自控制器
        [self setupSubViews];
        
        // 布局子控件
        [self makeSubViewsConstraints];
    }
    return self;
}
//-(void)setFrame:(CGRect)frame
//{
//    frame.size.height = WYMomentShareInfoViewHeight;
//    frame.size.width = Screen_Width - WYMomentContentLeftOrRightInset - WYMomentAvatarWH - WYMomentContentInnerMargin - WYMomentContentLeftOrRightInset;
//    [super setFrame:frame];
//}

- (void)setupSubViews{
    
    /// avatarView
    UIImageView *avatarView = [[UIImageView alloc] init];
    avatarView.userInteractionEnabled = YES;
    self.avatarView = avatarView;
    [self addSubview:avatarView];
    
    /// titleLabel
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.numberOfLines = 0;
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.font = SYSTEMFONT(13.0);
    self.titleLabel  = titleLabel;
    [self addSubview:titleLabel];
    
    /// detailLabel
    UILabel *detailLabel = [[UILabel alloc] init];
    detailLabel.textColor = color(112, 112, 112);
    detailLabel.numberOfLines = 0;
    detailLabel.textAlignment = NSTextAlignmentLeft;
    detailLabel.font = SYSTEMFONT(12.0);
    self.detailLabel  = detailLabel;
    [self addSubview:detailLabel];
    
    /// 播放按钮
    
    UIButton *playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [playBtn setImage:[UIImage imageNamed:@"GiftVideoPlayIcon_23x23"] forState:UIControlStateNormal];
    self.playBtn = playBtn;
    [avatarView addSubview:playBtn];
    
    UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] init];
    [tapGr addTarget:self action:@selector(tapAction:)];
    [self addGestureRecognizer:tapGr];
}

-(void)tapAction:(UITapGestureRecognizer *)tapGes
{
    [UIView animateWithDuration:.1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.backgroundColor = color(206, 210, 222);
    } completion:^(BOOL finished) {
        !self.touchBlock ? : self.touchBlock(self);
        self.backgroundColor = color(243, 243, 245);
    }];
}

-(void)setItemViewModel:(WYMomentItemViewModel *)itemViewModel
{
    _itemViewModel = itemViewModel;
    WYMomentShareInfo *shareInfo = itemViewModel.moment.shareInfo;
    [self.avatarView setImageWithURL:shareInfo.thumbImage placeholder:WYWebImagePlaceholder() options:YYWebImageOptionAllowInvalidSSLCertificates|YYWebImageOptionAllowBackgroundTask|YYWebImageOptionSetImageWithFadeAnimation completion:NULL];
    self.titleLabel.text = shareInfo.title;
    self.detailLabel.text = shareInfo.descr;
    
    // 更新一下布局
    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        CGFloat margin = -15;
        if (shareInfo.descr == nil || shareInfo.descr.length == 0) {
            margin = 0;
        }
        make.bottom.equalTo(self.avatarView.mas_bottom).offset(margin);
    }];
    
    self.playBtn.hidden = (shareInfo.shareInfoType != WYMomentShareInfoTypeMusic);
}

#pragma mark - 布局子控件
- (void)makeSubViewsConstraints{
    
    [self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(5);
        make.centerY.equalTo(self);
        make.width.and.height.mas_lessThanOrEqualTo(40);
    }];
    
    [self.playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.avatarView.mas_top);
        make.left.equalTo(self.avatarView.mas_right).offset(5);
        make.right.equalTo(self).offset(-5).priorityHigh();
    }];
    
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.avatarView.mas_bottom);
        make.left.equalTo(self.avatarView.mas_right).offset(5);
        make.right.equalTo(self.mas_right).offset(-5).priorityHigh();
    }];
}

@end
